package com.dada.tiku.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dada.tiku.pojo.Subject;
import com.dada.tiku.pojo.SubjectAnswer;
import com.dada.tiku.pojo.SubjectOptions;
import com.dada.tiku.pojo.TSubject;
import com.dada.tiku.pojo.TUser;
import com.dada.tiku.service.SubjectService;
import com.dada.tiku.utils.WxResult;
/*
 * 这是管理题目的controller
 */
@RestController
public class SubjectController {
	
	@Reference
	private SubjectService subjectService;
	
	/*
	 * 查询所有题目的方法---返回给前台是题目名称和选项对象的一个对象的集合
	 * [{id,title,[{id,name},{id,name},{id,name}]},
	 * {id,title,[{id,name},{id,name},{id,name}]}]
	 */
	@RequestMapping("/subject/query")
	public String querySubject(@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count,String openid){
		WxResult subjects = subjectService.queryAll(start,count,openid);
		String result = JSON.toJSONString(subjects);
		return result;
	}	
	
	/*
	 * 根据类型查找题目，页面可以选的，模拟题，真题，
	 */
	@RequestMapping("/subject/queryByCat")
	public String querySubjectByCategory(String category,@RequestParam(defaultValue="0")Integer start,
			@RequestParam(defaultValue="20")Integer count,String openid){
		List<Subject> subjects = subjectService.queryByCategory(category,start,count,openid);
		String result = JSON.toJSONString(subjects);
		return result;
	}
	
	/*
	 * 根据试卷id查找试卷写对应的试题
	 */
	@RequestMapping("/subject/paper")
	public String findPaperSubjectList(Integer paperid,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count,String openid){
		WxResult subjects = subjectService.findPaperSubjectList(paperid,start,count,openid);
		String result = JSON.toJSONString(subjects);
		return result;
	}
	
	/*
	 * 添加错题方法
	 */
	@RequestMapping("/subject/addError")
	public String addErrorSubject(Integer id,HttpServletRequest request,String openid){
		//错题要和用户相关联
		
		WxResult result = subjectService.addError(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 移除错题
	 */
	@RequestMapping("/subject/removeError")
	public String removeErrorSubject(Integer id,HttpServletRequest request,String openid){
		
		WxResult result = subjectService.removeError(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 添加收藏方法
	 */
	@RequestMapping("/subject/connection")
	public String connectionSubject(Integer id,HttpServletRequest request,String openid){
		
		WxResult result = subjectService.addConnection(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 取消收藏
	 */
	@RequestMapping("/subject/cancelConnection")
	public String cancelErrorSubject(Integer id,HttpServletRequest request,String openid){
		
		WxResult result = subjectService.cancelConnection(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 根据题目查询答案
	 */
	@RequestMapping("/subject/answer")
	public String findAnswer(Integer id){
		List<SubjectOptions> options = subjectService.findAnswer(id);
		return JSON.toJSONString(options);
	}
	
	/*
	 * 判断试题是否被用户收藏
	 */
	@RequestMapping("/subject/isCollection")
	public String isCollection(Integer id, String openid){
		WxResult result = subjectService.isCollection(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 查找用户所有收藏的试题集合
	 */
	@RequestMapping("/subject/getCollList")
	public String getCollectionList(String openid,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		WxResult result = subjectService.getCollectionList(openid,start,count);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 查找所有用户错题集合
	 */
	@RequestMapping("/subject/getErrorList")
	public String getErrorList(String openid,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		WxResult result = subjectService.getErrorList(openid,start,count);
		return JSON.toJSONString(result);
	}
	
}
